import { loginApi, logoutApi } from "@/api/auth";
import { 
    getUserInfoApi, 
    getCodeListApi, 
    getCheckCodeApi, 
    getMyVerifyListApi, 
    getHeJiCodeInfoApi, 
    getCodeMoneyDownApi, 
    invoicedownSubmitApi, 
    getIdcardCheckCodeApi,
    getUnlineCheckApi,
    getUpLoadApi,
} from "@/api/user";

import { resetRouter } from "@/router";
import { store } from "@/store";
import { defineStore } from 'pinia'

import { LoginData } from "@/api/auth/types";
import { UserInfo, Data } from "@/api/user/types";
import { log } from "console";

export const useUserStore = defineStore("user", () => {
    const user: UserInfo = {
        roles: [],
        perms: [],
    };

    /**
     * 登录
     *
     * @param {LoginData}
     * @returns
     */
    function login(loginData: LoginData) {
        return new Promise((resolve, reject) => {
            loginApi(loginData)
                .then((response) => {
                    console.log('res', response);
                    const { __token__ } = response.data;
                    //@ts-ignore
                    localStorage.setItem("token", __token__); // Bearer eyJhbGciOiJIUzI1NiJ9.xxx.xxx
                    resolve(response);
                })
                .catch((error) => {
                    reject(error);
                });
        });
    }

    // 获取信息(用户昵称、头像、角色集合、权限集合)
    function getUserInfo() {
        return new Promise<UserInfo>((resolve, reject) => {
            getUserInfoApi()
                .then(({ data }) => {
                    if (!data) {
                        reject("Verification failed, please Login again.");
                        return;
                    }
                    if (!data.roles || data.roles.length <= 0) {
                        reject("getUserInfo: roles must be a non-null array!");
                        return;
                    }
                    Object.assign(user, { ...data });
                    resolve(data);
                })
                .catch((error) => {
                    reject(error);
                });
        });
    }

    // user logout
    function logout() {
        return new Promise<void>((resolve, reject) => {
            logoutApi()
                .then(() => {
                    localStorage.setItem("token", "");
                    location.reload(); // 清空路由
                    resolve();
                })
                .catch((error) => {
                    reject(error);
                });
        });
    }

    // remove token
    function resetToken() {
        console.log("resetToken");
        return new Promise<void>((resolve) => {
            localStorage.setItem("token", "");
            resetRouter();
            resolve();
        });
    }

    /**
   * 核销码状态查询api
   *
   * @param {Data}
   * @returns
   */
    function codelist(Data: Data) {
        return new Promise((resolve, reject) => {
            getCodeListApi(Data)
            .then((response) => {
                console.log('response',response);

                resolve(response);
            })
            .catch((error) => {
                reject(error);
            });
        });
    }


    /**
   * 核销码核销api
   *
   * @param {Data}
   * @returns
   */
    function checkcode(Data: Data) {
        return new Promise((resolve, reject) => {
            getCheckCodeApi(Data)
            .then((response) => {
                console.log('response',response);

                resolve(response);
            })
            .catch((error) => {
                reject(error);
            });
        });
    }

    /**
   * 核销列表
   *
   * @param {Data}
   * @returns
   */
    function myverifylist(Data: Data) {
        return new Promise((resolve, reject) => {
            getMyVerifyListApi(Data)
            .then((response) => {
                console.log('response',response);

                resolve(response);
            })
            .catch((error) => {
                reject(error);
            });
        });
    }

    /**
   * 核销码查询合计订单全部核销码
   *
   * @param {Data}
   * @returns
   */
    function hejicodeinfo(Data: Data) {
        return new Promise((resolve, reject) => {
            getHeJiCodeInfoApi(Data)
            .then((response) => {
                console.log('response',response);

                resolve(response);
            })
            .catch((error) => {
                reject(error);
            });
        });
    }

    /**
   * 核销码查询合计订单全部核销码的总价
   *
   * @param {Data}
   * @returns
   */
    function codemoneydown(Data: Data) {
        return new Promise((resolve, reject) => {
            getCodeMoneyDownApi(Data)
            .then((response) => {
                console.log('response',response);

                resolve(response);
            })
            .catch((error) => {
                reject(error);
            });
        });
    }

    /**
   * 线下票打印
   *
   * @param {Data}
   * @returns
   */
    function invoicedownSubmit(data: Data) {
        return new Promise((resolve, reject) => {
            invoicedownSubmitApi(data)
                .then((response) => {
                    console.log("线下票打印data:", response);
                    resolve(response);
                })
                .catch((error) => {
                    reject(error);
                });
        });
    }

    /**
   * 核销码核销api
   *
   * @param {Data}
   * @returns
   */
    function idcardcheckcode(Data: Data) {
        return new Promise((resolve, reject) => {
            getIdcardCheckCodeApi(Data)
            .then((response) => {
                console.log('response',response);

                resolve(response);
            })
            .catch((error) => {
                reject(error);
            });
        });
    }

    /**
   * 线下优惠资质验证api
   *
   * @param {Data}
   * @returns
   */
    function unlinecheck(Data: Data) {
        return new Promise((resolve, reject) => {
            getUnlineCheckApi(Data)
            .then((response) => {
                console.log('response',response);

                resolve(response);
            })
            .catch((error) => {
                reject(error);
            });
        });
    }

    /**
   * 线下优惠资质验证api
   *
   * @param {Data}
   * @returns
   */
    function upload(Data: Data) {
        return new Promise((resolve, reject) => {
            getUpLoadApi(Data)
            .then((response) => {
                console.log('response',response);

                resolve(response);
            })
            .catch((error) => {
                reject(error);
            });
        });
    }

  return {
    user,
    login,
    getUserInfo,
    logout,
    resetToken,
    codelist,
    checkcode,
    myverifylist,
    hejicodeinfo,
    codemoneydown,
    invoicedownSubmit,
    idcardcheckcode,
    unlinecheck,
    upload,
  };
});

// 非setup
export function useUserStoreHook() {
  return useUserStore(store);
}


